package com.download.zhuaba.Utils;

import com.download.zhuaba.Dao.UserRepository;
import com.download.zhuaba.Entity.Music;
import com.download.zhuaba.Entity.User;
import com.download.zhuaba.ZhuabaApplication;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

@Component
@RunWith(SpringRunner.class)
@SpringBootTest(classes= ZhuabaApplication.class)
public class WelcomeUtil {
    public static Document web(String url){
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);//模拟火狐浏览器
        try {

            webClient.setJavaScriptTimeout(5000);
            webClient.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书
            webClient.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本
            webClient.getOptions().setCssEnabled(false);//禁用css支持

            webClient.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常
            webClient.getOptions().setTimeout(100000);//设置连接超时时间
            webClient.getOptions().setDoNotTrackEnabled(false);

            HtmlPage page = webClient.getPage(url);

            Thread.sleep(3000);
            String xml = page.asXml();
            Document document = Jsoup.parse(xml);//获取html文档
            return document;
        }
        catch (FailingHttpStatusCodeException e) {
            // TODO 自动生成的 catch 块
            // e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO 自动生成的 catch 块
            //e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            // e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            // e.printStackTrace();
        }finally {
            webClient.close();
        }
        return null;
    }
public static void main(String[] args) throws Exception {
        Document document = web("https://www.bilibili.com/v/popular/rank/all");//获取html文档
        Elements elements = document.getElementsByClass("info").select("a");
        Elements elements1 = document.getElementsByClass("num");
        Elements elements2 = document.getElementsByClass("data-box");
        int x=0;
        int y=0;
        for(int i=0;i<elements1.size();i++){
            String num =elements1.get(i).text();
            String title = elements.get(x).toString();
            String play =elements2.get(y).text();
            String comment =elements2.get(y+1).text();
            String up =elements.get(x+1).toString();
            x=x+2;
            y=y+3;
            //System.out.println(play);
            System.out.println(num+title+play+comment+up);
        }


    }
}
